package com.ccl.iot.dialogs;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ccl.iot.R;
import com.ccl.iot.publics.Window;

/**
 * 注册新用户的dialog
 */
public class UserRegisterDialog extends CustomDialog {

    public interface UserRegisterListener {
        public void onRegister(String aUserName, String aPassword, String aMobile, String aEMail, String aFPQuestion, String aFPAnswer);
    }

    private UserRegisterListener iRegisterListener = null;

    private boolean iCtlReady = false;

    private EditText iUser = null;
    private EditText iPassword = null;
    private EditText iPasswordConfirm = null;
    private EditText iMobile = null;
    private EditText iEMail = null;
    private EditText iFPQuestion = null;
    private EditText iFPAnswer = null;

    private Button iCancel = null;
    private Button iRegister = null;

    public UserRegisterDialog(Context context, UserRegisterListener aListener) {
        super(context, R.style.DialogTheme);

        iRegisterListener = aListener;
        initialize();
    }

    @Override
    public View getView() {
        return View.inflate(getContext(), R.layout.user_register_dialog, null);
    }

    private void initialize() {

        iUser = (EditText) this.findViewById(R.id.register_account);
        iPassword = (EditText) this.findViewById(R.id.register_password);
        iPasswordConfirm = (EditText) this.findViewById(R.id.register_passwordconfirm);
        iMobile = (EditText) this.findViewById(R.id.register_mobile);
        iEMail = (EditText) this.findViewById(R.id.register_email);
        iFPQuestion = (EditText) this.findViewById(R.id.register_fpquestion);
        iFPAnswer = (EditText) this.findViewById(R.id.register_fpanswer);

        iRegister = (Button) this.findViewById(R.id.register_register);
        iCancel = (Button) this.findViewById(R.id.register_cancel);

        iCtlReady = (iUser != null && iPassword != null && iPasswordConfirm != null &&
                iMobile != null && iEMail != null && iFPQuestion != null && iFPAnswer != null &&
                iRegister != null && iCancel != null);

        if (iCtlReady) {
            handleInputEvent();

            handleButtonEvent();
        }
    }

    private int getTextLength(TextView aText) {
        if (aText != null) {
            return aText.getText().length();
        }

        return 0;
    }

    private boolean isInfoValid() {
        if (getTextLength(iUser) > 0 && getTextLength(iPassword) > 0 && getTextLength(iPasswordConfirm) > 0) {
            if (iPassword.getText().toString().equals(iPasswordConfirm.getText().toString())) {
                if (getTextLength(iMobile) > 0 && getTextLength(iEMail) > 0 && getTextLength(iFPQuestion) > 0 && getTextLength(iFPAnswer) > 0) {
                    return true;
                }
            }
        }

        return false;
    }

    private void handleUsefulInfoChange(EditText aInfo) {
        if (aInfo != null) {
            aInfo.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start,
                                              int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start,
                                          int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (iCtlReady) {
                        iRegister.setEnabled(isInfoValid());
                    }
                }
            });
        }
    }

    private void handleInputEvent() {
        if (iUser != null) {
            iUser.requestFocus();

            Window.ShowInputDialog(getContext());

            handleUsefulInfoChange(iUser);
        }

        handleUsefulInfoChange(iPassword);
        handleUsefulInfoChange(iPasswordConfirm);
        handleUsefulInfoChange(iMobile);
        handleUsefulInfoChange(iEMail);
        handleUsefulInfoChange(iFPQuestion);
        handleUsefulInfoChange(iFPAnswer);
    }

    private void handleButtonEvent() {
        if (iCancel != null) {
            iCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UserRegisterDialog.this.dismiss();
                }
            });
        }

        if (iRegister != null) {
            iRegister.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isInfoValid()) {
                        if (iRegisterListener != null) {
                            iRegisterListener.onRegister(iUser.getText().toString(), iPassword.getText().toString(),
                                    iMobile.getText().toString(), iEMail.getText().toString(),
                                    iFPQuestion.getText().toString(), iFPAnswer.getText().toString());
                        }
                    }
                }
            });
        }
    }
}
